package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    /**
     * 分页查询护理计划
     *
     * @param name     护理计划名称
     * @param status   护理计划状态
     * @param pageNum  当前页码
     * @param pageSize 显示数据条数
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        Page<NursingPlanVo> page  = nursingPlanMapper.getByPage(name,status);

        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    /**
     * 添加护理计划
     *
     * @param nursingPlanDto 护理计划内容
     * @return
     */
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        //添加护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);
        //将护理计划的状态设置为启用
        nursingPlan.setStatus(1);
        nursingPlanMapper.addPlan(nursingPlan);
        Page<NursingPlanVo> byPage = nursingPlanMapper.getByPage(nursingPlan.getPlanName(), null);
        System.out.println(nursingPlan.getPlanName());
        //添加护理计划项目
        List<NursingProjectPlanVo> projectPlans = nursingPlan.getProjectPlans();
        for(NursingProjectPlanVo projectPlanVo : projectPlans){
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtil.copyProperties(projectPlanVo,nursingProjectPlan);
            nursingProjectPlan.setPlanId(byPage.get(0).getId());
            nursingPlanMapper.addProjectPlan(nursingProjectPlan);

        }
    }

    /**
     * 删除护理计划
     *
     * @param id 要删除的护理计划id
     * @return
     */
    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
        nursingPlanMapper.deleteProjectPlan(id);
    }

    /**
     * 根据id查询护理计划计划
     *
     * @param id 护理计划id
     * @return 护理计划详情
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtil.copyProperties(nursingPlan,nursingPlanVo);
        //查询护理计划项目
        List<NursingProjectPlanVo> projectPlanVos = nursingPlanMapper.getProjectPlan(id);
        for(NursingProjectPlanVo projectPlanVo: projectPlanVos){
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtil.copyProperties(projectPlanVo,nursingProjectPlan);
            //获取护理项目
            NursingProject nursingProject = nursingPlanMapper.getProjectById(nursingProjectPlan.getProjectId());
            //给护理项目找到名字
            projectPlanVo.setProjectName(nursingProject.getName());
        }
        nursingPlanVo.setProjectPlans(projectPlanVos);

        return nursingPlanVo;
    }

    /**
     * 编辑护理计划
     *
     * @param nursingPlanDto 护理计划信息
     * @return
     */
    @Override
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);

        //删除护理计划项目表中该计划原来执行的项目
        nursingPlanMapper.deleteProjectPlan(nursingPlanDto.getId());

        //得到编辑后该护理计划执行的护理项目
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        //插入到护理项目表中
        for (NursingProjectPlanDto projectPlan : projectPlans) {
            NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
            BeanUtil.copyProperties(projectPlan,nursingProjectPlan);
            nursingProjectPlan.setPlanId(nursingPlanDto.getId());
            nursingPlanMapper.addProjectPlan(nursingProjectPlan);
        }

        nursingPlanMapper.updatePlan(nursingPlan);
    }

    /**
     * 改变护理计划状态
     *
     * @param id     护理计划id
     * @param status 状态
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllPlan() {
        List<NursingPlanVo> nursingPlanVos = nursingPlanMapper.getAllPlan();
        return nursingPlanVos;
    }


}
